package com.webapp.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.webapp.domain.entity.Disputes;
import com.webapp.domain.entity.OneKeyMapping;
import com.webapp.domain.entity.Session;
import com.webapp.domain.vo.Pagination;

/**
 * 纠纷咨询DAO
 * @author zern
 *
 */
@Repository("disputesDAO")
public class DisputesDAO extends AbstractDAO<Disputes>{

	/**
	 * 删除纠纷咨询列表（伪删除）
	 * @param id
	 * @return
	 */
	public int deleteDisputes(String id){
		String hql = "update Disputes d set d.delFlag = '1' where d.id = :id";
		Query query = getSession().createQuery(hql);
		query.setString("id", id);
		return query.executeUpdate();
	}
	
	/**
	 * 批量删除纠纷咨询（伪删除）
	 * @param ids
	 * @return
	 */
	public int deleteDisputes(List<Long> ids){
		String hql = "update Disputes d set d.delFlag = '1' where d.id in (:id)";
		Query query = getSession().createQuery(hql);
		query.setParameterList("id", ids);
		return query.executeUpdate();
	}
	
	
	public List<OneKeyMapping> ability(String value){
		String hql="from OneKeyMapping o where o.disputeinfo=:disputeinfo";
		Query query=getSession().createQuery(hql);
		query.setString("disputeinfo", value);
		
		return query.list();
	}
	
	public String getMessageCount4User(Long disputesId){
		String sql = "SELECT COUNT(*) FROM MESSAGE M "
				+ "LEFT JOIN SESSION  S ON S.ID = M.SESSION_IgetDisputeAndMessageNumberD "
				+ "WHERE S.DISPUTES_ID = :id";
		Query query = getSession().createSQLQuery(sql);
		return query.uniqueResult().toString();
	}
	
	public Disputes getDisputes(DetachedCriteria detachedCriteria){
		return getByCriteria(detachedCriteria);
	}

	public List<Disputes> getDisputeAndMessageNumber(){
		List<Disputes> result=new ArrayList<Disputes>();
		String hql = "select d," +
				"(select count(*) from Session s where s.disputes.id=d.id) as number" +
				" from Disputes d " +
				" left join fetch d.disputesData " +
				" left join fetch d.proposer order by d.id" ;
		@SuppressWarnings("unchecked")
//		List<Object[]> d = (List<Object[]>) getHibernateTemplate().find(hql,null);
		List<Object[]> d = (List<Object[]>) find(hql);
		Disputes temp;
		for (Object[] object : d) {
			temp=(Disputes)object[0];
			temp.setMessageCount((Long)object[1]);
			result.add(temp);
		}
		return result;
	}
	
	/**
	 * 执行sql，返回long
	 * @param countHql
	 * @return
	 */
	public Long getCount(String countHql){
		Object object = getSession().createQuery(countHql).uniqueResult();
		return (Long)object;
	}
	
	/**
	 * 分页查询纠纷咨询列表
	 * @param messageQuery	获取未读消息的条件
	 * @param whereQuery	查询纠纷的条件
	 * @param p
	 * @return
	 */
	public Pagination<Disputes> paginateByQuery(String messageQuery, 
			String whereQuery,Pagination<Disputes> p){
		List<Disputes> result=new ArrayList<Disputes>();
		Long count = getCount("select count(*) from Disputes d "+whereQuery);
		p.setTotalCount(count);
		String hql = "select d," +
				"(select count(*) from Message m left join m.session s"
				+ " where s.disputes.id=d.id "+messageQuery+") as number"
				+ " from Disputes d "
				+ " left join fetch d.disputesData "
				+ " left join fetch d.user "
				+ " left join fetch d.proposer "
				+ " left join fetch d.arbdic "
				+ " left join fetch d.evaluate "
				+ " left join fetch d.mediation "
				+whereQuery;
		if(!StringUtils.isEmpty(p.getDirection())){
			if ("desc".equals(p.getDirection())) {
				hql = hql + " order by d."+p.getOrderBy()+" desc";
			}else {
				hql = hql + " order by d."+p.getOrderBy();
			}
		}
		Query query = getSession().createQuery(hql).setFirstResult(p.getPosStart())
				.setMaxResults(p.getPageSize());
		List<Object[]> d = query.list();
		Disputes temp;
		for (Object[] object : d) {
			temp=(Disputes)object[0];
			temp.setMessageCount((Long)object[1]);
			result.add(temp);
		}
		p.setData(result);
		return p;
	}
	
//	public Pagination<Session> 
	
}
